package com.future.hair.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.future.hair.server.config.jjwt.JwtHelper;
import com.future.hair.server.config.result.CommonResult;
import com.future.hair.server.config.result.ResultCode;
import com.future.hair.server.entity.Mall;
import com.future.hair.server.entity.Manager;
import com.future.hair.server.mapper.MallMapper;
import com.future.hair.server.mapper.ManagerMapper;
import com.future.hair.server.model.PageModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;



/**
*
* @author future-RogueYBJ
* @since 2020-05-29
* @version v1.0
*/

@RestController
@RequestMapping("//hair/manager")
@Api(tags = "Manager管理接口")

public class ManagerController{
    @Autowired
    private ManagerMapper mapper;

    @Autowired
    private MallMapper mallMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @ApiOperation(value = "查询 Manager 列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public CommonResult<List<Manager>> managerList(@RequestBody PageModel data,@RequestHeader String token) {
        if (null == data.getSize()){
            data.setSize(10);
        }
        if (null == data.getCurrent()){
            data.setCurrent(1);
        }
        return CommonResult.data(mapper.selectPage(new Page<>(data.getSize() , data.getCurrent()), null).getRecords());
    }

    @ApiOperation(value = "查询当前 Manager")
    @RequestMapping(value = "/id", method = RequestMethod.POST)
    public CommonResult<Manager> managerId(@RequestBody Manager data,@RequestHeader String token) {
        return CommonResult.data(mapper.selectById(data.getId()));
    }

    @ApiOperation(value = "增加 Manager")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult<Integer> create(@RequestBody Manager data,@RequestHeader String token) {
        if (null == data.getOpenId() || (null == data.getMobile() && null == data.getUsername()) || null == data.getPassword()){
            return CommonResult.failed("参数不能为空");
        }
        if (4444!=data.getOpenId() || 6666!=data.getOpenId()){
            return CommonResult.failed("添加失败");
        }
        if (JwtHelper.getByOpenId(token)<=6666 || 4444==data.getOpenId()){
            if (data.getMallId()==null){
                return CommonResult.failed("请选择门店");
            }
            QueryWrapper<Mall> mallWrapper = new QueryWrapper<Mall>();
            mallWrapper.eq("id", data.getMallId()); //请输入排异条件
            Mall mall = mallMapper.selectOne(mallWrapper);
            if (mall==null){
                return CommonResult.failed("请选择正确门店");
            }
        }

        if (managerExist(data)){
            data.setCreateTime(null);
            data.setUpdataTime(null);
            Integer result = mapper.insert(data);
            if (result == 1){
                return CommonResult.data(result);
            }else {
                return CommonResult.failed("创建失败");
            }
        }else{
            return CommonResult.failed(ResultCode.USER_EXIST);
        }
    }

    @ApiOperation(value = "更新 Manager")
    @RequestMapping(value = "/upData", method = RequestMethod.POST)
    public CommonResult<Integer> upData(@RequestBody Manager data, @RequestHeader String token) {
        Integer result = mapper.updateById(data);
        if (result==1){
            return CommonResult.data(result);
        }else {
            return CommonResult.failed("更新失败");
        }
    }

    @ApiOperation(value = "删除 Manager")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public CommonResult<Integer> delete(@RequestBody Manager data,@RequestHeader String token) {
        Integer result = mapper.deleteById(data.getId());
        if (result==1){
            return CommonResult.data(result);
        }else {
            return CommonResult.failed("删除失败");
        }
    }

    public Boolean managerExist (Manager data){
        QueryWrapper<Manager> wrapper1 = new QueryWrapper<Manager>();
        wrapper1.eq("mobile", data.getMobile()); //请输入排异条件
        Manager manager1 = mapper.selectOne(wrapper1);
        QueryWrapper<Manager> wrapper2 = new QueryWrapper<Manager>();
        wrapper2.eq("username", data.getUsername()); //请输入排异条件
        Manager manager2 = mapper.selectOne(wrapper2);
        return manager1==null && manager2==null;
    }
}